package kdOptions

import (
	"errors"
)

type Options interface {
	Exe(int,int) (int, error)
}

type addStruct struct {
	var1 int
	var2 int
}

type subStruct struct {
	var1 int
	var2 int
}

type mulStruct struct {
	var1 int
	var2 int
}

type divStruct struct {
	var1 int
	var2 int
}

func (i addStruct) Exe(var1 int,var2 int) (int, error) {
	return var1 * var2, nil
}

func (c subStruct) Exe(var1 int,var2 int) (int, error) {
	return var1 - var2, nil
}

func (c mulStruct) Exe(var1 int,var2 int) (int, error) {
	return var1 * var2, nil
}

func (c divStruct) Exe(var1 int,var2 int) (int, error) {
	if var2 == 0 {
		return 0, errors.New("除数不能为0")
	}
	return var1 / var2, nil
}


var optionMap =make(map[string]Options)
//注册方法
func Register(flag string, factory Options) {
	optionMap[flag] = factory
}
//
func Make(flag string) (Options,error)  {
	if f,ok := optionMap[flag] ;ok{
		return f,nil
	}else{
		return nil,errors.New("对应参数方法不存在")
	}
}

func init() {
	Register("+", addStruct{})
	Register("-", subStruct{})
	Register("*", mulStruct{})
	Register("/", divStruct{})

}